#pragma once
#include "SimpleCanvas.h"
#include "event/UIEvent.h"
#include "graphics/Rect.h"
#include "util/Flag.h"
#include "drawable/Drawable.h"
#include "include/core/SkCanvas.h"

namespace yzrilyzr_simplecanvas{
	struct MeasureSpec{
		static constexpr int const UNSPECIFIED=0;
		static constexpr int const AT_MOST=1;
		static constexpr int const EXACTLY=2;
		int mode;
		int32_t size;
	};

	EBCLASS(View){
		public:
		static constexpr int const VISIBLE=0;
		static constexpr int const INVISIBLE=4;
		static constexpr int const GONE=8;
		private:
		static constexpr int const PFLAG_INVALIDATED=0x1;
		static constexpr int const PFLAG_FORCE_LAYOUT=0x2;
		static constexpr int const FLAG_ENABLED=0x4;
		private:
		int32_t minWidth=0, minHeight=0;
		int32_t measuredWidth=0, measuredHeight=0;
		yzrilyzr_util::Flag flags;
		std::shared_ptr<View> parent=nullptr;
		Rect edgeDistance{0, 0, 0, 0};
		Rect padding{0, 0, 0, 0};
		Rect margin{0, 0, 0, 0};
		std::shared_ptr<Drawable> background=nullptr, foreground=nullptr;
		int visibility=VISIBLE;
		public:
		View();
		void draw(SkCanvas & canvas);
		virtual void onDrawBackground(SkCanvas & canvas);
		virtual void onDrawForeground(SkCanvas & canvas);
		bool dispatchDraw(SkCanvas & canvas);
		bool dispatchEvent(const UIEvent * event);
		void requestLayout();
		void layout(Rect & rect);
		void setPadding(Rect & rect);
		void setMargin(Rect & rect);
		void invalidate();
		void measure(MeasureSpec & widthMeasureSpec, MeasureSpec & heightMeasureSpec);
		void setMeasuredDimension(int32_t measuredWidth, int32_t measuredHeight);
		virtual void onDraw(SkCanvas & canvas);
		virtual void onLayout(bool changed, Rect & oldRect, Rect & newRect);
		virtual void onMeasure(MeasureSpec & widthMeasureSpec, MeasureSpec & heightMeasureSpec);
		int getFinalVisibility();
		int View::getVisibility();
		std::shared_ptr<View> getParent();
		void setEnabled(bool enable);
		bool isEnabled();
		Rect & getPadding();
		Rect & getMargin();

		void setBackground(std::shared_ptr<Drawable> v);
		void setForeground(std::shared_ptr<Drawable> v);

		static int32_t getDefaultSize(int32_t size, MeasureSpec & measureSpec);
		static int32_t px(int32_t dip);
	};
	typedef std::shared_ptr<View> ViewPtr;
}